/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package message_driven;

import entity.User;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.mail.Address;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.NamingException;
import session.manager.UserManagerBeanLocal;
import util.Wrapper;

/**
 *
 * @author giuseppe, antonio
 */
@MessageDriven(mappedName = "jms/queue/sendPassword", activationConfig = {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class SendPasswordBean implements MessageListener {

    @Resource(name = "mail/gmailAccount")
    private Session mailSession;
    @EJB
    UserManagerBeanLocal userManager;

    public SendPasswordBean() {
    }

    @Override
    public void onMessage(Message message) {
        ObjectMessage msg = (ObjectMessage) message;
        try {
            Wrapper wrapper = (Wrapper) msg.getObject();
            User user = userManager.find(wrapper.getString1());
            sendMail(user.getEmail(), "Nuova Password", message(wrapper.getString1(), wrapper.getString2()));

        } catch (NamingException ex) {
            Logger.getLogger(SendPasswordBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MessagingException ex) {
            Logger.getLogger(SendPasswordBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JMSException ex) {
            Logger.getLogger(SendPasswordBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void sendMail(String email, String subject, String body) throws NamingException, MessagingException {
        MimeMessage message = new MimeMessage(mailSession);
        Address toAddress = new InternetAddress(email);
        message.setRecipient(RecipientType.TO, toAddress);
        message.setSubject(subject);
        message.setText(body);
        message.saveChanges();
        Transport tr = mailSession.getTransport();
        String serverPassword = mailSession.getProperty("mail.password");
        tr.connect(null, serverPassword);
        tr.sendMessage(message, message.getAllRecipients());
        tr.close();
    }

    private String message(String user, String password) {
        StringBuilder sb = new StringBuilder();
        sb.append("Gentile ");
        sb.append(user);
        sb.append("/n");
        sb.append("La sua nuova password è: ");
        sb.append(password);
        return sb.toString();
    }
}